package com.jswl.training.springjpa.controller;

import com.jswl.training.springjpa.common.state.Result;
import com.jswl.training.springjpa.entity.User;
import com.jswl.training.springjpa.entity.vo.UserVo;
import com.jswl.training.springjpa.service.UserService;
import com.querydsl.core.QueryResults;
import com.querydsl.core.Tuple;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {


    private  UserService userService;

    public UserController(UserService userService){
        this.userService=userService;
    }

    /**
     * RequestParam 接收参数
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @RequestMapping("/getUsers")
    public Result getUsers(@RequestParam("pageIndex") Integer pageIndex,@RequestParam("pageSize") Integer pageSize){
        Page<User> userPage =userService.getPage(pageIndex,pageSize);

        return Result.ok(userPage);
    }

    /**
     * 前端传递json对象
     * @param user
     * @return
     */
    @PostMapping("/saveJsonToUser")
    public Result saveJsonToUser(@RequestBody User user){
        userService.saveUser(user);
        return Result.ok(user);
    }

    /**
     * 普通对象接收
     * @param user
     * @return
     */
    @PostMapping("/saveUser")
    public Result saveUser( User user){
        userService.saveUser(user);
        return Result.ok(user);
    }

    /**
     * 接收url路径接收
     * @param userId
     * @param userName
     * @return
     */
    @PostMapping("/pathParam/{userId}/{userName}")
    public Result pathParam(@PathVariable Integer userId,@PathVariable String userName){
        log.info("前端path传递数据: userId={},userName={}",userId,userName);
        return Result.ok("");
    }

    /**
     * RequestHeader 从 请求header中接收指定参数
     * @param auth
     * @return
     */
    @PostMapping("/headerParam")
    public Result headerParam(@RequestHeader("auth") String auth){
        log.info("前端Header传递数据: auth",auth);
        return Result.ok("");
    }




    @RequestMapping("/getByUserName")
    public Result getByUserName(@RequestParam("userName") String userName){
       //return Result.ok(userService.findByUserName(userName));

        return Result.ok(userService.getUsersBy(userName));
    }

    @RequestMapping("/queryDSL")
    public Result queryDSL(@RequestParam("userName") String userName,@RequestParam("phone") String phone){
        Page<UserVo> userVos = userService.getUserVoByUserName(userName);
        return Result.ok(userVos);
        //return Result.ok(userService.getByUserNameAndPhone(userName,phone));
    }

    @RequestMapping("/frameencode")
    public Result frameencode(){
        return Result.ok(null);
    }


}
